import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

typedef OnPickImageCallback = void Function(ImagePicker picker, ImageSource source);

class ImagePickerUtil {
  static final ImagePicker _picker = ImagePicker();

  static Future<void> displayPickImageDialog(BuildContext context, OnPickImageCallback onPick,
      {String? title}) async {
    return showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: title != null
                ? Text(title)
                : Text(
                    '请选择图片',
                    style: TextStyle(fontSize: 16),
                  ),
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  TextButton(
                      child: const Text('拍照'),
                      onPressed: () {
                        onPick(_picker, ImageSource.camera);
                        Navigator.of(context).pop();
                      }),
                  TextButton(
                      child: const Text('从图库选择'),
                      onPressed: () {
                        onPick(_picker, ImageSource.gallery);
                        Navigator.of(context).pop();
                      }),
                ],
              ),
            ],
          );
        });
  }
}
